package model;

import javax.jws.soap.SOAPBinding;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDao {

    //1、根据userId来查到永用户信息
    public User getUserById(int userId){
        Connection connection=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        try {
            connection=DBUtil.getConnection();
            String sql="select * from user where userId=?";
            ps=connection.prepareStatement(sql);
            ps.setInt(1,userId);
            rs=ps.executeQuery();
            if (rs.next()){
                User user=new User();
                user.setUserId(rs.getInt("userId"));
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                return user;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DBUtil.close(connection,ps,rs);
        }
        return null;
    }
    //2、根据username来查到对应的用户信息
    public User getUserByName(String username){
        Connection connection=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        try {
            connection=DBUtil.getConnection();
            String sql="select * from user where username=?";
            ps=connection.prepareStatement(sql);
            ps.setString(1,username);
            rs=ps.executeQuery();
            if (rs.next()){
                User user=new User();
                user.setUserId(rs.getInt("userId"));
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                return user;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DBUtil.close(connection,ps,rs);
        }
        return null;
    }

}




















